package ServerConnection;

import Client.ServerInfo;
import Message.AccountLoginInfo;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class AccountLoginServer implements Runnable {
    DatabaseServer databaseServer;

    public AccountLoginServer(DatabaseServer databaseServer) {
        this.databaseServer = databaseServer;
    }

    public AccountLoginServer() {
        databaseServer = new DatabaseServer();
    }

    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(ServerInfo.SERVER_LOGIN_PORT);
            System.out.println("用户登录的服务成功启动！");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                Thread handleLogin = new Thread(new ThreadHandleAccountLogin(socket, databaseServer));
                handleLogin.start();
            }
        } catch (IOException e) {
            System.out.println("WARNING:AccountLoginServer Init fail,Reason:socket has ioException.");
        }
    }


}

class ALIS {
    public static void main(String[] args) {
        try {
            DatabaseServer databaseServer = new DatabaseServer();
            ServerSocket serverSocket = new ServerSocket(ServerInfo.SERVER_LOGIN_PORT);
            System.out.println("处理用户登录事件的服务成功启动！");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                Thread handleLogin = new Thread(new ThreadHandleAccountLogin(socket, databaseServer));
                handleLogin.start();
            }
        } catch (IOException e) {
            System.out.println("WARNING:AccountLoginServer Init fail,Reason:socket has ioException.");
        }
    }
}
 class ThreadHandleAccountLogin implements Runnable {
    Socket socket = null;
    AccountLoginInfo info;
    boolean res;
    String userId;
    String userPassword;
    DatabaseServer databaseServer;

    public ThreadHandleAccountLogin(Socket socket, DatabaseServer databaseServer) {
        super();
        this.socket = socket;
        this.databaseServer = databaseServer;
    }

    @Override

    public void run() {
        try {
            System.out.println("收到用户的登录请求，正在处理");
            //获取从客户端得到的消息，并且执行了消息。
            InputStream inputStream = socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            info = (AccountLoginInfo) objectInputStream.readObject();
            userId = info.getId();
            userPassword = info.getPassword();
            res = databaseServer.doLogin(userId,userPassword);
            System.out.println("用户登录结果：" + res);
            //返还执行的结果到客户端。
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(String.valueOf(res).getBytes(StandardCharsets.UTF_8));

            inputStream.close();
            objectInputStream.close();
            outputStream.close();
            socket.close();


        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}